package vcp.client.GUI.mvc.view.addons;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.LineBorder;

import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;

import vcp.client.GUI.mvc.view.custom.CustomCalendarIconButton;
import vcp.client.utility.Common;

import com.toedter.calendar.JCalendar;

/**
 * @author VCP team
 *         <p>
 *         This class is responsible of building window GUI which will allows
 *         the user to choose the appropriated date from calendar.
 *         </p>
 * 
 * @see CustomCalendarIconButton
 */
public class JCalendarWrapper extends JDialog implements ActionListener
{
	private static final long serialVersionUID = 1L;

	public static String getCalendarDateFormat ()
	{
		return "dd/MM/yyyy";
	}

	/** This variable is a reference to JCalender */
	private JCalendar calendar;

	/** This variable is a reference to hosting button */
	private final CustomCalendarIconButton hostButton;

	public JCalendarWrapper ( CustomCalendarIconButton hostButton )
	{
		super((JFrame) null, true);
		this.setSize(new Dimension(364, 243));
		this.initialize();
		this.hostButton = hostButton;
	}

	// ****************************************************************************************************************************
	// Private methods
	// ****************************************************************************************************************************

	/** This method presents the calendar to user */
	private void initialize ()
	{
		this.getContentPane().setLayout(new BorderLayout(0, 0));

		JPanel panel = new JPanel();
		panel.setBorder(new LineBorder(new Color(0, 0, 0)));
		this.getContentPane().add(panel, BorderLayout.CENTER);
		panel.setLayout(new BorderLayout(0, 0));

		this.calendar = new JCalendar();
		this.calendar.getMonthChooser().getComboBox().setFont(new Font("Tahoma", Font.PLAIN, 11));
		this.calendar.setSundayForeground(Color.black);
		panel.add(this.calendar);

		JPanel panel_1 = new JPanel();
		this.getContentPane().add(panel_1, BorderLayout.SOUTH);
		panel_1.setLayout(new BorderLayout(0, 0));

		JButton btnCancel = new JButton(Common.BTN_CANCEL);
		panel_1.add(btnCancel, BorderLayout.WEST);
		btnCancel.setFont(new Font("Tahoma", Font.PLAIN, 14));
		btnCancel.setActionCommand(Common.BTN_CANCEL);

		JButton btnOk = new JButton(Common.BTN_OK);
		panel_1.add(btnOk, BorderLayout.EAST);
		btnOk.setFont(new Font("Tahoma", Font.PLAIN, 14));
		btnOk.setActionCommand(Common.BTN_OK);
		btnOk.addActionListener(this);
		btnCancel.addActionListener(this);

		this.setLocationRelativeTo(null);
	}

	// ****************************************************************************************************************************
	// ActionListener methods
	// ****************************************************************************************************************************

	@Override
	public void actionPerformed ( ActionEvent event )
	{
		if ( event.getActionCommand().equalsIgnoreCase(Common.BTN_OK) )
		{
			this.hostButton
					.getBindingTextField()
					.setText(
						DateTimeFormat
								.forPattern(JCalendarWrapper.getCalendarDateFormat())
								.print(new DateTime(this.calendar.getDate()))
					);
		}

		this.hostButton.dispose();
	}
}
